package com.haixue.android.academy.view.tiku;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;

public class TextDrawable extends Drawable {

    private final String text;
    private final Paint paint;
    private float fontHeight;

    private int height=50;
    private int width=100;
    
    private RectF rectF;
    
    public TextDrawable(int textSize,String text) {

        this.text = text;

        this.paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setTextSize(textSize);
        paint.setAntiAlias(true);
        paint.setFakeBoldText(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextAlign(Paint.Align.CENTER);


        rectF=new RectF(0,0,width,height);

    }

    
    public TextDrawable(int width,int height,int textSize,String text) {

        this.text = text;

        this.paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setTextSize(textSize);
        paint.setAntiAlias(true);
        paint.setFakeBoldText(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextAlign(Paint.Align.CENTER);
        
        this.width=width;
        this.height=height;
        
        rectF=new RectF(0,0,width,height);

    }
    
    
    @Override
    public void draw(Canvas canvas) {
    	
    	paint.setColor(0xffb2b2b2);
    	canvas.drawRoundRect(rectF, height/5, height/5, paint);
    	
    	paint.setColor(Color.WHITE);
    	
    	
        FontMetrics fontMetrics = paint.getFontMetrics(); 
        fontHeight = fontMetrics.bottom - fontMetrics.top; 
        
        float textBaseY = height - (height - fontHeight) / 2 - fontMetrics.bottom; 
        
        canvas.drawText(text, width / 2, textBaseY, paint);
        
    }

    @Override
    public void setAlpha(int alpha) {
        paint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        paint.setColorFilter(cf);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }


}